<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Shuka189\Controller;

/**
 * Description of Shoppingcart
 *
 * @author SoChishun
 */
class Shoppingcart extends Appbase {

    public function index() {
        $cart = session('cart');
        if (!$cart) {
            $this->error('无效订单!', U('Index/index'));
        }
        $id = key($cart);
        $mProduct = new \XWAM\Model\ProductGoodsModel();
        $product = $mProduct->where(array('id' => $id))->field('title, price')->find();
        $data = array_merge(current($cart), $product);
        $data['subtotal'] = $data['price'] * $data['number'];
        $data['serial_no'] = 'C' . $data['id'] . '-' . $data['number'] . '-' . date('YmdHis');
        $this->assign('data', $data);
        $this->display();
    }

    public function index2() {
        $cart = session('cart');
        if (!$cart) {
            $this->error('无效订单!', U('Index/index'));
        }
        $list = array(); // product_name, product_number, product_
        $mProduct = new \XWAM\Model\ProductGoodsModel();
        $products = $mProduct->where(array('id' => array('in', array_keys($cart))))->field('id, price')->select();
        foreach ($cart as $id => $item) {
            $aitem = $item;

            if ($products && array_key_exists($id, $products)) {
                
            }
        }
        $this->assign('list', $list);
        $this->display();
    }

    public function create() {
        $data = I('post.');
        $is_member = I('is_member');
        $real_name = I('real_name');
        $email = I('email');
        $user_name = I('user_name');
        $password = I('password');
        $pay_method = I('pay_method');
        $id = I('id');
        $number = I('number');

        if ($is_member == '1') {
            $msg = '';
            $mLogin = new \XWAM\Model\LoginModel();
            if (!$mLogin->login($user_name, $password, null, $msg)) {
                $this->error($msg, '', 1);
            }
        }
        /*
          $data=session('cart');
          if($data && array_key_exists($id, $data)){
          $data[$id]['number']+=$number;
          }else{
          $data[$id]=array('is_member'=>$is_member,'real_name'=>$real_name,'email'=>$email,'user_name'=>$user_name,'number'=>$number);
          }
         */
        $data = array($id => array('id' => $id, 'pay_method' => $pay_method, 'is_member' => $is_member, 'real_name' => $real_name ? $real_name : $user_name, 'email' => $email, 'number' => $number));
        session('cart', $data);
        redirect(U('index'));
    }

}
